
#include "Tileset.h"

bool operator <(const std::string& a, const std::string& b){
	return (a[0] <= b[0]);
}

Tileset::Tileset(){
	
	tileSize = 0;
	tilesetWidth = 0;
	tilesetHeight = 0;
}

bool Tileset::loadTilesetDef(char* fileName){
	
	std::ifstream fileStream(fileName);

	if( !fileStream.good() || fileStream.fail() )
		return false;

	while( fileStream.peek() == ';' || fileStream.peek() == '\n' )
		fileStream.ignore(256,'\n');

	fileStream >> std::skipws >> tileSize;

	while( fileStream.peek() == ';' || fileStream.peek() == '\n' )
		fileStream.ignore(256,'\n');

	fileStream >> std::skipws >> tilesetWidth;

	while( fileStream.peek() == ';' || fileStream.peek() == '\n' )
		fileStream.ignore(256,'\n');

	fileStream >> std::skipws >> tilesetHeight;

	while( fileStream.peek() == ';' || fileStream.peek() == '\n' )
		fileStream.ignore(256,'\n');

	std::string str;
	char c_str[40];
	int pos;

	while(!fileStream.eof())
	{
		fileStream >> std::skipws >> c_str; //Tile name
		fileStream >> std::skipws >> pos;   //Tile position

		str = c_str;

		tilesetMap[str] = pos;

		while( fileStream.peek() == ';' || fileStream.peek() == '\n' )
			fileStream.ignore(256,'\n');
	}

	fileStream.close();

	return true;
}

void Tileset::cleanup(){
	
	tilesetMap.clear();
}

int Tileset::getTile(char* tileName){
	
	std::string str;
	str = tileName;

	return tilesetMap[str];
}
